
local function crystal_ingot()
    local craft = {}
    local b0 = " "
    m1 = "default:mese_crystal"
    m2 = "ethereal:crystal_spike"
    m3 = "bucket:bucket_water"
    craft[1] = {m1, m2, m2}
    craft[2] = {m1, m3, b0}
    craft[3] = {b0, b0, b0}
    return craft
end

-- set m1row pattern
local function m1row(m1, m2, m3, m4)
    local craft = {}
    local b0 = " "
    craft[1] = {m1, m1, m1}
    craft[2] = {m2, m3, m4}
    craft[3] = {b0, b0, b0}
    return craft
end

local function touch_response(dev, msg)
    if msg["key_enter_field"] == nil and msg["quit"] ~= nil then
    -- ignore the event when a player closes menu
        return
    end
    if msg["onoff"] ~= nil then
        mem.state = not mem.state
        if mem.state then
            digiline_send("lcd", "Ice machine started.")
        else 
            digiline_send("lcd", "Ice machine stopped.")
        end
    elseif msg["key_enter_field"] == "vanilla" then
        local s1 = "farming:vanilla"
        local s2 = s1;
        local s3 = "farming:bottle_ethanol"
        local s4 = "bucket:bucket_water"
        digiline_send(dev, m1row(s1, s2, s3, s4))
    elseif msg["key_enter_field"] == "minty" then
        local s1 = "farming:mint_leaf"
        local s2 = "bucket:bucket_water"
        local s3 = "farming:juicer"
        local s4 = "vessels:drinking_glass"
        digiline_send(dev, m1row(s1, s2, s3, s4))
    elseif msg["key_enter_field"] == "soymilk" then
        local s1 = "farming:soy_beans"
        local s2 = "farming:vanilla_extract"
        local s3 = "bucket:bucket_water"
        local s4 = "vessels:drinking_glass"
        digiline_send(dev, m1row(s1, s2, s3, s4))
    elseif msg["key_enter_field"] == "crystalingot" then
        digiline_send(dev, crystal_ingot())
    end
    local quantity = msg[msg["key_enter_field"]]
    if tonumber(quantity) ~= nil then
        for i=1, quantity, 1 do
            digiline_send(dev, "single")
        end
    end
end

local function spawn_button(name, label, x, y)
    local btn_width = 3
    local btn_height = 1
    local button = {}
    button.command = "addbutton"
    button["X"] = x
    button["Y"] = y
    button["W"] = btn_width
    button["H"] = btn_height
    button["name"] = name
    button["label"] = label
    return button
end

local function spawn_field(name, label, x, y)
    local height = 1
    local width = 3
    local field = {}
    field.command = "addfield"
    field["X"] = x
    field["Y"] = y
    field["W"] = width
    field["H"] = height
    field["name"] = name
    field["label"] = label
    -- have 16 water buckets stored in autocrafter
    field["default"] = "16"
    return field
end


local function touch_init3(chnl, state)
    local reset = {}
    reset.command = "clear"
    digiline_send(chnl, reset)

    local guide = {}
    guide.command = "addlabel"
    guide["X"] = 2.5
    guide["Y"] = 0
    guide.label = "Press ENTER (or RETURN) key to place order."
    digiline_send(chnl, guide)

    local button = {}
    button.command = "addbutton"
    button["X"] = 0
    button["Y"] = 0.5
    button["W"] = 10
    button["H"] = 1
    button["name"] = "onoff"
    if state then
        button["label"] = "Stop Ice Machine"
    else
        button["label"] = "Start Ice Machine"
    end
    digiline_send(chnl, button)
    
    local offset_x = 1
    local offset_y = 1.5
    local choices = {}
    choices[1] = {name = "vanilla", label = "Vanilla Extract"}
    choices[2] = {name = "minty", label = "Mint Tea"}
    choices[3] = {name = "soymilk", label = "Soy Milk"}
    choices[4] = {name = "crystalingot", label = "Crystal Ingot"}
     for i, v in ipairs(choices) do
        local f = spawn_field(v.name, v.label, 
                              offset_x, i + offset_y)
        digiline_send(chnl, f)
    end
end

local function touch_init2(channel)
    local reset = {}
    reset.command = "clear"
    digiline_send(channel, reset)
    
    local choices = {}
    local c = {}
    c.name = "vanilla"; c.label = "Vanilla Extract"
    choices[1] = c
    c.name = "minty";   c.label = "Mint Tea"
    choices[2] = c
    for i, v in ipairs(choices) do
        local btn = spawn_button(v.name, v.label, x % 6, 0 + i)
        digiline_send(channel, btn)
    end
end

local function main()
    local CONST_T = 1
    local CONST_T_ICE = 5
    local ts = "ts"   -- touchscreen
    local ac = "ac"   -- autocrafter
    if event.type == "program" then
        mem.state = false
        mem.timer = 0
        touch_init3(ts, mem.state)
    elseif event.type == "digiline" and event.channel == ts then
        touch_response(ac, event.msg)
        touch_init3(ts, mem.state)
    end
    if event.type == "interrupt" 
    or event.type == "program" then
        port.c = not port.c
        mem.timer = mem.timer + 1 % CONST_T_ICE;
        if mem.timer % CONST_T_ICE == 0  and mem.state then
            port.a = not port.a
        end
        interrupt(CONST_T)
    end
end
return main()
